<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

      <el-form-item label="用户姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="用户姓名"></el-input>
      </el-form-item>

      <el-form-item label="登录昵称" prop="nickName">
        <el-input v-model="dataForm.nickName" placeholder="登录昵称"></el-input>
      </el-form-item>

      <el-form-item label="所属部门" prop="deptName">
        <el-popover
          ref="deptListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="deptList"
            :props="deptListTreeProps"
            node-key="id"
            ref="deptListTree"
            @current-change="deptListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.deptName" v-popover:deptListPopover :readonly="true" placeholder="点击选择部门" class="menu-list__input"></el-input>
      </el-form-item>

      <el-form-item label="登录账号" prop="loginAccount">
        <el-input v-model="dataForm.loginAccount" placeholder="登录账号"></el-input>
      </el-form-item>

    <el-form-item label="手机号码" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号码"></el-input>
    </el-form-item>

    <el-form-item label="邮箱地址" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱地址"></el-input>
    </el-form-item>

    <el-form-item label="出生日期" prop="birthday">
      <el-date-picker
          v-model="dataForm.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生日期">
      </el-date-picker>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>

  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: null,
          name: '',
          deptId: '',
          deptName: '',
          loginAccount: '',
          mobile: '',
          email: '',
          birthday: '',
          nickName: ''
        },
        dataRule: {
          name: [
            { required: true, message: '用户姓名不能为空', trigger: 'blur' }
          ],
          nickName: [
            { required: true, message: '登录昵称不能为空', trigger: 'blur' }
          ],
          deptName: [
            { required: true, message: '部门不能为空', trigger: 'blur' }
          ],
          loginAccount: [
            { required: true, message: '登录账号不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱地址不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '用户出生日期', trigger: 'blur' }
          ]
        },
        deptList: [],
        deptListTreeProps: {
          label: 'deptName',
          children: 'children'
        }
      }
    },
    methods: {
      init (id) {
        this.$http({
          url: this.$http.adornUrl('/user/dept/list/tree'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.deptList = treeDataTranslate(data.data, 'id')
        })

        this.dataForm.id = id || undefined
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.user.name
                this.dataForm.loginAccount = data.user.loginAccount
                this.dataForm.mobile = data.user.mobile
                this.dataForm.email = data.user.email
                this.dataForm.birthday = data.user.birthday
                this.dataForm.nickName = data.user.nickName
                this.dataForm.deptId = data.user.deptId
                this.dataForm.deptName = data.user.deptName
              }
            })
          }
        })
      },
      // 菜单树选中
      deptListTreeCurrentChangeHandle (data, node) {
        this.dataForm.deptId = data.id
        this.dataForm.deptName = data.deptName
      },
      // 菜单树设置当前选中节点
      deptListTreeSetCurrentNode () {
        this.$refs.deptListTree.setCurrentKey(this.dataForm.deptId)
        this.dataForm.deptName = (this.$refs.deptListTree.getCurrentNode() || {})['deptName']
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'deptId': this.dataForm.deptId,
                'deptName': this.dataForm.deptName,
                'loginAccount': this.dataForm.loginAccount,
                'mobile': this.dataForm.mobile,
                'email': this.dataForm.email,
                'birthday': this.dataForm.birthday,
                'nickName': this.dataForm.nickName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
